package com.zx.sdk.util;

import android.app.Application;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ToggleButton;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.window.EasyWindow;
import com.zx.sdk.R;
import com.zx.sdk.ZxSDK;

import java.util.ArrayList;

public class LogHelper {
    public static final String KEY_ADV_LOGS = "KEY_ADV_LOGS";
    public static final String TAG = "ZxSDK";
    private static Adapter adapter = null;
    private static EasyWindow easyWindow = null;
    private static Application application = null;
    private static RecyclerView logsRv = null;

    public static void init(Application context) {
        application = context;
        if (SPHelper.getSp().getBoolean(KEY_ADV_LOGS, false)) {
            showLogs();
        }
    }

    public static void d(String... msg) {
        if (!ZxSDK.isDebug()) return;
        if (msg.length == 1) {
            Log.d(TAG, msg[0]);
            addLogToRv(msg[0]);
            return;
        }
        StringBuilder append = new StringBuilder();
        for (String s : msg) {
            append.append("【").append(s).append("】");
        }
        Log.d(TAG, append.toString());
        addLogToRv(append.toString());
    }

    private static void addLogToRv(String log) {
        RunnableHelper.runOnUIThread(() -> {
            if (logsRv != null && adapter != null) {
                adapter.addLog(log);
                logsRv.scrollToPosition(adapter.getItemCount());
            }
        });
    }

    public static void e(String... error) {
        if (!ZxSDK.isDebug()) return;
        if (error.length == 1) {
            Log.e(TAG, error[0]);
            return;
        }
        StringBuilder append = new StringBuilder();
        for (String s : error) {
            append.append("【").append(s).append("】");
        }
        Log.e(TAG, append.toString());
        addLogToRv(append.toString());
    }

    public static void e(String from, Throwable exception) {
        if (ZxSDK.isDebug()) {
            ToastHelper.showToast(TAG + "_" + from + ":" + exception.getMessage());
        }
        e(Log.getStackTraceString(exception));
        addLogToRv(Log.getStackTraceString(exception));
    }

    public static void showLogs() {
        if (easyWindow != null) {
            easyWindow.cancel();
        }
        easyWindow = new EasyWindow<>(application)
                .setContentView(R.layout.log_layout)
                .setHeight(UiHelper.getScreenHeight() / 2);
        easyWindow.show();
        logsRv = easyWindow.getContentView().findViewById(R.id.logsRv);
        easyWindow.getContentView().findViewById(R.id.clearBtn).setOnClickListener(v -> {
            adapter.clear();
        });
        ToggleButton resizeBtn = easyWindow.getContentView().findViewById(R.id.resizeBtn);
        resizeBtn.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                easyWindow.setHeight(UiHelper.dp2Px(60));
                easyWindow.setWidth(UiHelper.dp2Px(80));
            } else {
                easyWindow.setHeight(UiHelper.getScreenHeight() / 2);
                easyWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            }
        });
        adapter = new Adapter();
        logsRv.setAdapter(adapter);
    }

    public static class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private final ArrayList<String> logs = new ArrayList<>();

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new RecyclerView.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.logs_item, parent, false)) {
            };
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            ((TextView) holder.itemView.findViewById(R.id.logTv)).setText(logs.get(position));

        }

        @Override
        public int getItemCount() {
            return logs.size();
        }

        public void addLog(String log) {
            logs.add(log);
            notifyItemRangeInserted(logs.size(), 1);
        }

        public void clear() {
            logs.clear();
            notifyDataSetChanged();
        }
    }
}
